<template>
  <div class="">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '../welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="15">
          <el-input
            clearable
            placeholder="请输入内容"
            @input="searcha"
            v-model="queryInfo.query"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searcha"
            ></el-button></el-input
        ></el-col>
        <el-col :span="6"
          ><el-button type="primary" @click="dialogVisible = true"
            >添加用户</el-button
          ></el-col
        >
      </el-row>
      <!-- 添加的弹框 -->
      <el-dialog title="添加用户" :visible.sync="dialogVisible" width="50%">
        <el-form
          :model="addUserForm"
          :rules="addUserFormRules"
          ref="addUserFormRef"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addUserForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="addUserForm.password"></el-input> </el-form-item
          ><el-form-item label="邮箱" prop="email">
            <el-input v-model="addUserForm.email"></el-input> </el-form-item
          ><el-form-item label="电话" prop="mobile">
            <el-input v-model="addUserForm.mobile"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 表格 -->
      <el-table :data="users" border style="width: 100%">
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="username" label="姓名" width="120">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="200">
        </el-table-column>
        <el-table-column prop="mobile" label="电话" width="200">
        </el-table-column>
        <el-table-column prop="role_name" label="角色" width="120">
        </el-table-column>
        <el-table-column label="状态" width="150">
          <template slot-scope="scope">
            <div>
              <el-switch
                v-model="scope.row.mg_state"
                active-color="#13ce66"
                inactive-color="#ff4949"
              >
              </el-switch>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="170" style="text-align: center">
          <template slot-scope="scope">
            <!-- 编辑用户 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="Edit(scope.row)"
            ></el-button>
            <!-- 删除用户 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.row.id)"
              circle
            ></el-button>
            <!-- 分配权限 -->
            <el-button type="warning" icon="el-icon-s-tools" circle></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 编辑的弹框 -->

      <el-dialog title="这是编辑用户的" :visible.sync="EditDia" width="50%">
        <el-form
          :model="addUserForm"
          :rules="addUserFormRules"
          ref="editUserFormRef"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addUserForm.username" disabled></el-input>
          </el-form-item>
          ><el-form-item label="邮箱" prop="email">
            <el-input v-model="addUserForm.email"></el-input> </el-form-item
          ><el-form-item label="电话" prop="mobile">
            <el-input v-model="addUserForm.mobile"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="EditSubmitForm">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[1, 2, 3, 4, 5]"
        :page-size="queryInfo.pagesize"
        :current-page="queryInfo.pagenum"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { lodash } from "lodash";
import { UserListApi, UserAddApi, UserEditApi, UserDelApi } from "@/http/api";
export default {
  data() {
    // 验证邮箱
    const checkEmail = (rule, value, callback) => {
      // 正则验证
      const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      if (!reg.test(value)) {
        callback(new Error("该邮箱不合法 请正确输入 "));
      }
      callback();
    };
    // 验证手机号
    const checkMobile = (rule, value, callback) => {
      const reg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (!reg.test(value)) {
        callback(new Error("请正确输入手机号"));
      }
      callback();
    };
    return {
      users: [],
      total: 0,
      queryInfo: {
        // query: "",
        pagenum: 1,
        pagesize: 5,
      },
      dialogVisible: false, //添加弹框的
      EditDia: false, //编辑弹框的
      addUserForm: {}, // 添加用户的数据
      addUserFormRules: {
        username: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 5, max: 15, message: "长度在 6 到 15个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],

        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" },
        ],

        mobile: [
          { required: true, message: "请输入电话", trigger: "blur" },
          { validator: checkMobile, trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.UserListApi();
  },
  methods: {
    async UserListApi() {
      const res = await UserListApi(this.queryInfo);
      console.log(res);
      this.userList = res;
      let { total, users } = res;
      this.users = users;
      this.total = total;
    },
    // 搜索
    searcha: _.debounce(function () {
      this.UserListApi();
    }, 300),
    // 弹出编辑框
    Edit(row) {
      this.addUserForm = row;
      this.EditDia = true;
    },
    // 编辑确定的按钮
    EditSubmitForm() {
      // 验证
      this.$refs.editUserFormRef.validate(async (valid) => {
        if (!valid) return false;

        // 调用接口 // 更新
        const { id, mobile, email } = this.addUserForm;
        await UserEditApi(id, { mobile, email });

        //隐藏、、
        this.EditDia = false;

        this.UserListApi();
      });
    },

    //添加弹框确定按钮
    submitForm() {
      this.$refs.addUserFormRef.validate(async (valid) => {
        // 不合法返回false
        if (!valid) return false;
        // 调用接口 提交给后台
        await UserAddApi(this.addUserForm);
        // console.log(this.addUserForm);

        // UserListApi(this.user);
        // 隐藏弹框
        this.dialogVisible = false;
        // 更新
        this.UserListApi();
        // let user = {
        //   username: "",
        //   email: "",
        //   password: "",
        //   mobile: "",
        // };
      });
    },
    handleSizeChange(size) {
      this.queryInfo.pagesize = size;
      this.UserListApi();
    },
    handleCurrentChange(num) {
      this.queryInfo.pagenum = num;
      this.UserListApi();
    },
    // components: {},

    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await UserDelApi(id);
          this.UserListApi();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang='scss'>
.box-card {
  margin-top: 20px;
  .el-input {
    // width: 120px;
  }
}
.el-table {
  margin-top: 20px;
  text-align: center;
}
.el-main[data-v-1d9b105c] {
  // text-align: center;
  line-height: 0px;
}
</style>
